package com.longlong.controller.frontDeskApi;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longlong.config.globalExceptionClass.BusinessException;
import com.longlong.entity.RegistrationCompetition;
import com.longlong.entity.RegistrationForm;
import com.longlong.entity.WxGzhUser;
import com.longlong.mapper.RegistrationCompetitionMapper;
import com.longlong.mapper.RegistrationFormMapper;
import com.longlong.mapper.RegistrationRacetrackMapper;
import com.longlong.mapper.WxGzhUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/font")
@Slf4j
public class FCompetitionController {


    @Autowired
    private RegistrationCompetitionMapper registrationCompetitionMapper;
    @Autowired
    private WxGzhUserMapper wxGzhuserMapper;
    @Autowired
    private RegistrationFormMapper registrationFormMapper;

    @Autowired
    private RegistrationRacetrackMapper registrationRacetrackMapper;

    /**
     * 微信小程序主页获取全部赛事信息
     * @return
     */
    @PostMapping("/getCompetitionList")
    public Object getCompetitionList(){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<RegistrationCompetition> rFWrapper = new QueryWrapper<>();
        LocalDateTime now = LocalDateTime.now();
        String localTime = df.format(now);
        rFWrapper.lt("competition_start",localTime);
        rFWrapper.gt("competition_finish",localTime);
        List<RegistrationCompetition> registrationCompetitions = registrationCompetitionMapper.selectList(rFWrapper);
        return registrationCompetitions;
    }

    /**
     * 判断用户是否可以报名
     */
    @GetMapping("/isPossibleRegister")
    public Object isPossibleRegister(String openId,Integer competitionId){
        //根据openid获取用户id
        //获取用户id
        QueryWrapper<WxGzhUser> wXWrapper = new QueryWrapper<>();
        wXWrapper
                .eq("openid_gzh",openId);
        Integer userId = wxGzhuserMapper.selectOne(wXWrapper).getId();
        //根据用户id和赛事id查询报名表，如果查询出来就返回true
        QueryWrapper<RegistrationForm> rFWrapper = new QueryWrapper<>();
        rFWrapper.eq("user_id",userId)
                .eq("competition_id",competitionId);
        if(registrationFormMapper.selectList(rFWrapper) == null){
            return new RegistrationForm();
        }

        return registrationFormMapper.selectList(rFWrapper);
    }

    /**
     * 获取赛道信息
     * @return
     */
    @GetMapping("/getRegistrationRacetrack")
    public Object getRegistrationRacetrack(){
        return registrationRacetrackMapper.selectList(null);
    }

    /**
     * 提交报名表单
     * @param registrationForm
     * @param openId
     * @return
     */
    @GetMapping("/submitRegistrationForm")
    public Object submitRegistrationForm(RegistrationForm registrationForm,String openId,String title1,String title2){
        //将赛道和题目转换为数组用于添加报名表单
        String[] racetracks = registrationForm.getRacetrack().split(",");
        //获取用户id
        QueryWrapper<WxGzhUser> WxGzhUserWrapper = new QueryWrapper<>();
        WxGzhUserWrapper
                .eq("openid_gzh",openId);
        Integer userId = wxGzhuserMapper.selectOne(WxGzhUserWrapper).getId();
        if(userId == null){
            throw new BusinessException(404,"请用微信公众号进行赛事报名提交");
        }
        registrationForm.setUserId(userId);
        //查询是否存在此用户的此赛程报名表
        QueryWrapper<RegistrationForm> RegistrationFormWrapper = new QueryWrapper<>();
        RegistrationFormWrapper
                .eq("user_id",registrationForm.getUserId())
                .eq("competition_id",registrationForm.getCompetitionId());
        List<RegistrationForm> flog = registrationFormMapper.selectList(RegistrationFormWrapper);
        if(racetracks.length == 1){
            //报名信息填写一个 数据库没有
            //报名信息填写一个 数据库一个
            //报名信息填写一个 数据库两个
            if(flog.size() > 0) {
                //删除报名信息
                registrationFormMapper.delete(RegistrationFormWrapper);
            }
            registrationForm.setTitle(title1);
            return registrationFormMapper.insert(registrationForm);
        }else if(racetracks.length == 2){
            //定义两个报名表
            RegistrationForm registrationForm1 = new RegistrationForm();
            registrationForm1.setCompetitionId(registrationForm.getCompetitionId());
            registrationForm1.setUserId(registrationForm.getUserId());
            registrationForm1.setName(registrationForm.getName());
            registrationForm1.setAge(registrationForm.getAge());
            registrationForm1.setTelephone(registrationForm.getTelephone());
            registrationForm1.setIndustryExperience(registrationForm.getIndustryExperience());
            registrationForm1.setTitle(title1);
            registrationForm1.setRacetrack(racetracks[0]);
            RegistrationForm registrationForm2 =  new RegistrationForm();
            registrationForm2.setCompetitionId(registrationForm.getCompetitionId());
            registrationForm2.setUserId(registrationForm.getUserId());
            registrationForm2.setName(registrationForm.getName());
            registrationForm2.setAge(registrationForm.getAge());
            registrationForm2.setTelephone(registrationForm.getTelephone());
            registrationForm2.setIndustryExperience(registrationForm.getIndustryExperience());
            registrationForm2.setRacetrack(racetracks[1]);
            registrationForm2.setTitle(title2);
            //报名信息填写两个 数据库没有
            //报名信息填写两个 数据库一个
            //报名信息填写两个 数据库两个
            if(flog.size() > 0){
                registrationFormMapper.delete(RegistrationFormWrapper);
            }
            registrationFormMapper.insert(registrationForm1);
            return registrationFormMapper.insert(registrationForm2);
        }
        throw new BusinessException(404,"系统错误请联系管理员");
    }



    /**
     * 获取用户已经报名过的信息
     * 前端传递的body类型 competitionId(比赛的id) openId(用户id)
     * @return
     */
    @GetMapping("/getUserRegistrationInfo")
    public Object getUserRegistrationInfo(String competitionId,String openId){
        //根据openid获取用户id
        QueryWrapper<WxGzhUser> wrapper = new QueryWrapper<>();
        wrapper.eq("openid_gzh",openId);
        Integer wxGzhUserId = wxGzhuserMapper.selectOne(wrapper).getId();
        //根据用户的id和赛事id查询用户填写的报名信息
        QueryWrapper<RegistrationForm> RegistrationFormWrapper = new QueryWrapper<>();
        RegistrationFormWrapper
                .eq("user_id",wxGzhUserId)
                .eq("competition_id",competitionId);
        List<RegistrationForm> registrationForms = registrationFormMapper.selectList(RegistrationFormWrapper);
        if(registrationForms == null){
            throw new BusinessException(201,"null");
        }
        return registrationForms;
    }


    /**
     * 上传图片
     */
    @RequestMapping("/upload")
    public Object upload(String filePath1, String filePath2, Integer competitionId, String openId,String declarationFilePath){
        //根据openid获取用户id
        QueryWrapper<WxGzhUser> wrapper = new QueryWrapper<>();
        wrapper.eq("openid_gzh",openId);
        Integer wxGzhUserId = wxGzhuserMapper.selectOne(wrapper).getId();
        //根据用户的id和赛事id查询用户填写的报名信息
        QueryWrapper<RegistrationForm> RegistrationFormWrapper = new QueryWrapper<>();
        RegistrationFormWrapper
                .eq("user_id",wxGzhUserId)
                .eq("competition_id",competitionId);
        //获取所有报名信息
        List<RegistrationForm>registrationForm = registrationFormMapper.selectList(RegistrationFormWrapper);
        if(registrationForm.size() == 1){
            //报名信息只有一个
            registrationForm.get(0).setDeclaration(declarationFilePath);
            registrationForm.get(0).setSubmitCompleted(1);
            registrationForm.get(0).setWork(filePath1);
            return registrationFormMapper.updateById(registrationForm.get(0));
        }else if(registrationForm.size() == 2){
            //报名信息有两个
            registrationForm.get(0).setDeclaration(declarationFilePath);
            registrationForm.get(1).setDeclaration(declarationFilePath);
            registrationForm.get(0).setSubmitCompleted(1);
            registrationForm.get(1).setSubmitCompleted(1);
            registrationForm.get(0).setWork(filePath1);
            registrationForm.get(1).setWork(filePath2);
            registrationFormMapper.updateById(registrationForm.get(1));
            return registrationFormMapper.updateById(registrationForm.get(0));
        }
        throw new BusinessException(404,"系统错误请联系管理员");
    }
}




























